﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;
using HCFramework.API.LINQ;
using System.Data;

namespace HCFramework.AdminTool.LanguageManagement
{
    public partial class Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {

            }
        }
        protected void RadGridMain_PreRender(object sender, EventArgs e)
        {
            LoadGridData();
        }
        public void LoadGridData()
        {
            HCDataContext db = new HCDataContext();
            var result = from a in db.Languages
                         select a;
            if (txtFilterVN.Text != "") result = from r in result where r.VietNam.Contains(txtFilterVN.Text) select r;
            if (txtFilterEN.Text != "") result = from r in result where r.English.Contains(txtFilterEN.Text) select r;
            DataTable dt = M.Common.LINQToDataTable(result.OrderBy(ik => ik.English));
            RadGridMain.DataSource = dt;
            RadGridMain.DataBind();
        }
        protected void RadGridMain_ItemCommand(object source, Telerik.Web.UI.GridCommandEventArgs e)
        {
            //if (e.CommandName == "Xoa")
            //{
            //    GridDataItem item = (GridDataItem)e.Item;
            //    RoleController.Delete(Guid.Parse(item["RoleID"].Text));
            //}
        }

        protected void LinkButtonDelete_Click(object sender, EventArgs e)
        {
            foreach (GridItem gridItem in RadGridMain.SelectedItems)
            {
                GridDataItem dataItem = (GridDataItem)gridItem;
                int id = int.Parse(dataItem["LanguageID"].Text);

                C.LanguageController.Delete(id);
            }
        }

        protected void LinkButtonUpdate_Click(object sender, EventArgs e)
        {
            foreach (GridDataItem item in RadGridMain.Items)
            {
                int id = int.Parse(item["LanguageID"].Text);
                string english = (item.FindControl("txtEnglish") as TextBox).Text;
                HCDataContext db = new HCDataContext();
                Language lang = C.LanguageController.GetByID(id, db);
                lang.English = english;
                C.LanguageController.Update(lang, db);
            }
        }
    }
}